import { _decorator, Component, Node, Button, Label } from 'cc';
import { BaseView } from '../../../../scripts/libs/rxjs/cc3/BaseView';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { udm } from '../../../../scripts/managers/UserDataManager';
import { am } from '../../../../scripts/managers/AudioManager';
import { misc_sdk_m } from '../../../../scripts/managers/SDK/MiscSDKManager';
import { find } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SettingsGroup')
export class SettingsGroup extends BaseView {

    @property(Button)
    musicButton: Button = null;

    @property(Button)
    soundButton: Button = null;

    @property(Button)
    hapticButton: Button = null;

    useObserves() {
        return [
            BindButton(this.musicButton).subscribe(this.musicButtonClicked.bind(this)),
            BindButton(this.soundButton).subscribe(this.soundButtonClicked.bind(this)),
            BindButton(this.hapticButton).subscribe(this.hapticButtonClicked.bind(this)),
            udm.bgmOn.subscribe(this.refreshBGM.bind(this)),
            udm.sfxOn.subscribe(this.refreshSFX.bind(this)),
            udm.vibrateOn.subscribe(this.refreshVibrate.bind(this)),
        ];
    }

    refreshBGM() {
        find('on', this.musicButton.node).active = udm.bgmOn.value;
        find('off', this.musicButton.node).active = !udm.bgmOn.value;
    }

    refreshSFX() {
        find('on', this.soundButton.node).active = udm.sfxOn.value;
        find('off', this.soundButton.node).active = !udm.sfxOn.value;
    }

    refreshVibrate() {
        find('on', this.hapticButton.node).active = udm.vibrateOn.value;
        find('off', this.hapticButton.node).active = !udm.vibrateOn.value;
    }

    musicButtonClicked() {
        this.log('bgmButtonClicked');
        udm.bgmOn.value = !udm.bgmOn.value;
        udm.save();
    }

    soundButtonClicked() {
        this.log('sfxButtonClicked');
        udm.sfxOn.value = !udm.sfxOn.value;
        udm.save();
        if (udm.sfxOn.value) {
            am.play.execute('ab_lobby/Audio/sfx/ButtonClick');
        }
    }

    hapticButtonClicked() {
        this.log('vibrateButtonClicked');
        udm.vibrateOn.value = !udm.vibrateOn.value;
        udm.save();
        misc_sdk_m.requestHapic('light');
    }

}

